#include <iostream>
#include <map>
using namespace std;

// 72.77 %
class Solution
{
  public:
	bool isHappy(int n)
	{
		map<int, int> myMap;
		int temp;
		while (myMap[n] <= 1)
		{
			if (n == 1)
				return 1;
			myMap[n]++;
			temp = 0;
			while (n)
			{
				temp += (n % 10) * (n % 10);
				n /= 10;
			}
			n = temp;
		}
		return false;
	}
};

int main()
{
	int n = 19;
	cout << Solution().isHappy(n) << endl;
	return 0;
}
